package com.tshopping.service.impl;

import com.tshopping.bean.Page;
import com.tshopping.bean.Store;
import com.tshopping.dao.GoodsDao;
import com.tshopping.dao.StoreDao;
import com.tshopping.dao.UserDao;
import com.tshopping.service.StoreService;
import com.tshopping.util.FileUploader;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @name: StoreServiceImpl
 * @description: 店铺业务层
 * @author: Jackeys
 * @date: 2021-01-22 16:00
 */
@Service(value = "storeService")
@Transactional
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreDao storeDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private UserDao userDao;
    @Override
    /**
     * @description 处理用户申请店铺
     * @Param: user
     * @Param: address
     * @Param: name
     * @Param: description
     * @Param: picture
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-22 16:02
     */
    public ResponseData addStore(int user, String address, String name, String description, String picture) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",storeDao.addStore(user, address, name, description, picture) && userDao.updateUserRole(3,user));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户修改店铺
     * @Param: address
     * @Param: name
     * @Param: description
     * @Param: picture
     * @Param: user
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-22 16:03
     */
    public ResponseData updateStore(String address, String name, String description, String picture,int isApprove, int user, int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",storeDao.updateStoreByUserIdAndStoreId(address, name, description, picture,isApprove, user, id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户删除店铺
     * @Param: user
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-22 16:03
     */
    public ResponseData deleteStore( int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",storeDao.deleteStore(id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户查询店铺
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-22 16:03
     */
    public ResponseData selectStore(int user) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("store",storeDao.selectStoreByUserId(user));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户查询店铺
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 10:04
     */
    public ResponseData selectStoreById(int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("store",storeDao.selectStoreByStoreId(id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户上传店铺头像
     * @Param: request
     * @Param: email
     * @Param: file
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-22 17:19
     */
    public ResponseData uploadStoreCoverFile(HttpServletRequest request, String email, MultipartFile file) {
        ResponseData responseData=ResponseData.ok();
        String directoryPath = request.getSession().getServletContext().getRealPath("resource/users/" + email + "/storePicture/");
        String originalFileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + originalFileName;
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/users/" + email + "/storePicture/" + newFileName;
        if (FileUploader.upload(directoryPath, newFileName, file)) {
            responseData.putDataValue("pictureURL", path);
        } else {
            responseData.putDataValue("pictureURL", null);
        }
        return responseData;
    }

    @Override
    /**
     * @description 返回店铺的所有信息
     * @Param: store
     * @Param: user
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 21:51
     */
    public ResponseData selectStoreInformation(int store,int user,int currentPage) {
        ResponseData responseData=ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = goodsDao.selectCountByStoreAndUser(store,user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("store",storeDao.selectStoreByStoreId(store));
        responseData.putDataValue("page", page);
        responseData.putDataValue("goodsList", goodsDao.selectGoodsByStoreIDAndUserID(store, user, start, size));
        return responseData;
    }

    @Override
    /**
     * @description 返回所有店铺
     * @Param: currentPage
     * @return
     * @author Jackeys
     * @date 2021-02-01 16:52
     */
    public ResponseData getAllStore(int currentPage) {
        ResponseData responseData=ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = storeDao.selectStoreCount();
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("storeList", storeDao.selectAllStore(start,size));
        return responseData;
    }

    @Override
    /**
     * @description 审核店铺
     * @Param: isApprove
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-02-01 16:53
     */
    public ResponseData approveStore(int isApprove, int id) {
        ResponseData responseData=ResponseData.ok();
        int user=storeDao.selectUserByStoreId(id);
        responseData.putDataValue("flag",storeDao.updateStoreApprove(isApprove,id)&&userDao.updateUserRole(1,user));
        return responseData;
    }
}
